package com.my.kq.controller;

import com.my.kq.entity.Attendance;
import com.my.kq.entity.Employee;
import com.my.kq.service.EmployeeService;
import com.my.kq.service.attendanceService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

@WebServlet("/attendanceaddxb")
public class attendanceaddxb extends HttpServlet {
    private attendanceService attendanceService=new attendanceService();
    private EmployeeService EmployeeService=new EmployeeService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf-8");
            resp.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            HttpSession session = req.getSession();
            Employee cook =(Employee) session.getAttribute("cook");
            Integer id = cook.getId();
            Employee yee= EmployeeService.Empcx(id);
            //员工编号
            Integer number = yee.getDepartment_number();

            //日期
            LocalDate now1 = LocalDate.now();
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String day = df.format(now1);
         Attendance att= attendanceService.attcx(number,day);
            Integer attid = att.getId();


            //时间
            LocalTime now2 = LocalTime.now();
            DateTimeFormatter df2 = DateTimeFormatter.ofPattern("HH:mm:ss");
            String startTime = df2.format(now2);
        //    SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
       //     String startTime = sdf1.format(now2);
            String[] split = startTime.split(":");

            //打卡状态
            String starTime="";
            if(Integer.parseInt(split[0])>=18&&Integer.parseInt(split[0])<19){
                starTime="正常";
                session.setAttribute("str",starTime);
            }
            if(Integer.parseInt(split[0])>=17&&Integer.parseInt(split[0])<18){
                starTime="早退";
                session.setAttribute("str",starTime);
            }

        //    if(Integer.parseInt(split[0])>=17&&Integer.parseInt(split[0])<=19){
                attendanceService.attenaddxw(attid,startTime,starTime);
          //  }



            req.getRequestDispatcher("jsp/welcome.jsp").forward(req,resp);



        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
